package com.lsz.crm.controller;

import com.lsz.crm.base.BaseController;
import com.lsz.crm.base.ResultInfo;
import com.lsz.crm.exceptions.ParamsException;
import com.lsz.crm.model.UserModel;
import com.lsz.crm.pojo.User;
import com.lsz.crm.query.UserQuery;
import com.lsz.crm.service.UserService;
import com.lsz.crm.utils.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author liushizhong
 * @Date 2021/10/7 21:31
 * @Version 1.0
 */

@Controller
@RequestMapping("user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;


    @RequestMapping("test")
    public String test(){
        throw new ParamsException("参数异常！");
    }

    @PostMapping("login")
    @ResponseBody
    public ResultInfo userLogin(String userName,String userPwd){

        ResultInfo resultInfo = new ResultInfo();
        //调用service 登录方法
        UserModel userModel = userService.userLogin(userName, userPwd);
        //返回结果对象
        resultInfo.setResult(userModel);

        return resultInfo;
    }

    @PostMapping("updatePwd")
    @ResponseBody
    public ResultInfo updateUserPassWord(HttpServletRequest request,String oldPwd,
                                         String newPwd,String repeatPwd){
        ResultInfo resultInfo = new ResultInfo();

        // 获取cookie中的userId
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
        // 调用 service 层修改密码操作
        userService.updatePassWord(userId,oldPwd,newPwd,repeatPwd);

        return resultInfo;
    }

    @RequestMapping("updatePWDPage")
    public String toPasswordPage(){

        return "user/password";
    }


    /**
     * 查询所有销售人员
     * @return
     */
    @RequestMapping("queryAllSales")
    @ResponseBody
    public List<Map<String,Object>> queryAllSales(){
        return userService.queryAllSales();
    }

    /**
     * 多条件查询用户
     * @param userQuery
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> queryUserByParams(UserQuery userQuery) { return userService.queryUserByParams(userQuery);
    }

    /**
     * 进⼊⽤户⻚⾯
     * @return
     */
    @RequestMapping("index")
    public String index(){

        return "user/user";
    }

    /**
     * 添加⽤户
     * @param user
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public ResultInfo addUser(User user) {
        userService.addUser(user);
        return success("⽤户添加成功！");
    }

    /**
     * 更新⽤户
     * @param user
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public ResultInfo updateUser(User user) {
        userService.updateUser(user);
        return success("⽤户更新成功！");
    }

    /**
     * 删除⽤户
     * @param ids
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public ResultInfo deleteUser(Integer[] ids){
        userService.deleteUserByIds(ids);
        return success("⽤户记录删除成功");
    }

    /**
     * 进⼊⽤户添加或更新⻚⾯
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("toAddOrUpdateUserPage")
    public String addUserPage(Integer id, Model model){
        if(null != id){
            // 更新编辑用户信息
            model.addAttribute("userModel",userService.selectByPrimaryKey(id));
    }
        return "user/addAndUpdateUser";
    }

    @RequestMapping("toSettingPage")
    public String toSettingPage(HttpServletRequest request,Model model){
        // 获取cookie中的userId
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
        // 封装 model
        model.addAttribute("userModel",userService.selectByPrimaryKey(userId));
        return "user/message";
    }
}
